package com.wechart.service.manage.impl;

import com.wechart.dao.domain.Orgaccount;
import com.wechart.dao.domain.OrgaccountCriteria;
import com.wechart.dao.mapper.OrgaccountMapper;
import com.wechart.model.ManagerModel;
import com.wechart.service.manage.OrgaccountService;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service(value = "orgaccountService")
public class OrgaccountServiceImpl implements OrgaccountService {

    @Autowired
    private OrgaccountMapper orgaccountMapper;

    @Override
    public List<Orgaccount> getOrgaccount(ManagerModel managerModel) {
        OrgaccountCriteria orgaccountCriteria=new OrgaccountCriteria();
        OrgaccountCriteria.Criteria criteria=orgaccountCriteria.createCriteria();
        if(StringUtils.isNoNullString(managerModel.getOrgcode())){
            criteria.andOrgcodeEqualTo(StringUtils.cutDateStr(managerModel.getOrgcode()));
        }
        if(StringUtils.isNoNullString(managerModel.getState())){
            criteria.andStateEqualTo(Short.parseShort(StringUtils.cutDateStr(managerModel.getState())));
        }
        if (managerModel.getOrgcodelist().size()>0 && !managerModel.getAreacode().equals("3300")){
            criteria.andOrgcodeIn(managerModel.getOrgcodelist());
        }
        if(StringUtils.isNoNullString(managerModel.getAccount())){
            criteria.andAccountEqualTo(managerModel.getAccount());
        }
        orgaccountCriteria.setOrderByClause("id asc");
        return orgaccountMapper.selectByExample(orgaccountCriteria);
    }
    /**
     * 入网机构账户信息插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertOrgaccount(ManagerModel managerModel) {
        OrgaccountCriteria orgaccountCriteria=new OrgaccountCriteria();
        Orgaccount data = new Orgaccount();
//        data.setId(Integer.parseInt(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
        data.setPrtry(managerModel.getPrtry());
        data.setPrtryname(managerModel.getPrtryname());
        data.setBankcode(managerModel.getBankcode());
        data.setAcctbank(managerModel.getAcctbank());
        data.setAccount(managerModel.getAccount());
        data.setAcctname(managerModel.getAcctname());
        data.setOrgauthcode(managerModel.getOrgauthcode());
        data.setSubclass(managerModel.getSubclass());
        data.setState(Short.parseShort(managerModel.getState()));
        data.setBookmode(Short.parseShort(managerModel.getBookmode()));
        return orgaccountMapper.insertSelective(data);
    }

    /**
     * 入网机构账户修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyOrgaccount(ManagerModel managerModel ,int id) {
        OrgaccountCriteria orgaccountCriteria=new OrgaccountCriteria();
        orgaccountCriteria.createCriteria().andIdEqualTo(id);
        Orgaccount data = new Orgaccount();
//        data.setId(Integer.parseInt(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
        data.setPrtry(managerModel.getPrtry());
        data.setPrtryname(managerModel.getPrtryname());
        data.setBankcode(managerModel.getBankcode());
        data.setAcctbank(managerModel.getAcctbank());
        data.setAccount(managerModel.getAccount());
        data.setAcctname(managerModel.getAcctname());
        data.setOrgauthcode(managerModel.getOrgauthcode());
        data.setSubclass(managerModel.getSubclass());
        data.setState(Short.parseShort(managerModel.getState()));
        data.setBookmode(Short.parseShort(managerModel.getBookmode()));
        return orgaccountMapper.updateByExampleSelective(data,orgaccountCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Orgaccount getOrgaccountById(int id) {
        OrgaccountCriteria orgaccountCriteria=new OrgaccountCriteria();
        orgaccountCriteria.createCriteria().andIdEqualTo(id);
        List<Orgaccount> data = orgaccountMapper.selectByExample(orgaccountCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }


}
